#!/bin/sh
#
# linuxrc - Internet Tablet's initscript
#
# Copyright (C) 2007 Instituto Nokia de Tecnologia
#
# Author: Osvaldo Santana Neto <osvaldo.neto@openbossa.org>
#         Abner Silva <abner.silva@openbossa.org>
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation version 2 of the License.
#
#  This program is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#

#set -vx

if [ -f /etc/linuxrc.conf ]; then
    . /etc/linuxrc.conf
fi

main() {
    show_image "$BOOT_IMAGE"

    # system
    mount_system_filesystem
    create_devices "$COMMON_ROOT/dev"
    initialize_coredump "$COREDUMPS_DIR"

    # energy
    initialize_watchdogs
    initialize_powersave_conf
    initialize_ondemand

    # devices
    initialize_wlan
    initialize_bluetooth

    # rootfs
    mount_rootfs
    initialize_rootfs
    export_initfs_bin

    /usr/sbin/dsmetool --root-mounted # TODO: replace

    boot
}


_mountfs() {
    fstype="$1"
    fsdev="$2"
    target="$3"
    if [ "$4" ]; then
        options="-o $4"
    else
        options=""
    fi
    mkdir -p "$target"
    mount $options -t $fstype $fsdev $target
    if ! grep "$target" /proc/mounts > /dev/null 2>&1; then
        exit 1
    fi
}

show_image() {
    echo -n "Showing the boot image..."

    img="$1"
    /usr/bin/show_image -i $img -n # TODO: replace

    echo "done."
}

mount_system_filesystem() {
    echo -n "Mounting system filesystem..."

    _mountfs proc  none /proc
    _mountfs sysfs none /sys
    _mountfs tmpfs none /tmp size=$TEMP_SIZE

    echo "done."
}

create_devices() {
    echo -n "Creating devices..."

    devdir="$1"

    mkdir -p "$devdir"
    for device in $DEVICES; do
        name=`echo $device | sed 's/\(.*\)_.*$/\1/'`
        args=`echo $device | sed 's/^.*_\(.*\)/\1/' | sed 's@/@ @g'`
        minor=`sed -n "s/^\(.*\) ${name}$/\1/p" /proc/misc`
        if [ ! "$minor" ]; then
            continue
        fi
        if [ ! -e "$devdir/$name" ]; then
            mknod "$devdir/$name" $args $minor
        fi
    done

    echo "done."
}

initialize_coredump() {
	echo -n "Enabling core dumps..."

	coredir="$1"
	echo "$coredir/%e-%s-%p.core" > /proc/sys/kernel/core_pattern
	ulimit -c unlimited > /dev/null 2>&1

	echo "done."
}

initialize_watchdogs() {
    echo -n "Starting Watchdog kicker..."

    # TODO: replace this section with ksdogs
    #dsme -d -l syslog -v 4 -p /usr/lib/dsme/libstartup.so > /dev/null 2>&1
    #/usr/sbin/waitfordsme > /dev/null 2>&1
    #/usr/sbin/dsmetool -r "/usr/bin/bme_`grep product /proc/component_version | cut -c 13-`" > /dev/null 2>&1
    #/usr/sbin/dsmetool -t "/usr/sbin/kicker" > /dev/null 2>&1
    # /TODO

    /usr/sbin/kdogsd -d 2> /dev/null

    echo "done."
}

initialize_powersave_conf() {
    echo -n "Setting power save configurations..."

    if [ -f /sys/devices/system/timer/timer0/dyn_tick ]; then
        echo 1 > /sys/devices/system/timer/timer0/dyn_tick
    fi
    if [ -f  /sys/power/sleep_while_idle ]; then
        echo 1 > /sys/power/sleep_while_idle
    fi

    echo "done."
}

initialize_ondemand()
{
    echo -n "Enabling ondemand governor..."

	if [ -f /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor ]; then
	    echo ondemand > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
	fi

    echo "done."
}

initialize_wlan() {
    echo -n "Initializing WLAN..."

    insmod $MODULE_PATH/umac.ko # TODO: replace
    insmod $MODULE_PATH/cx3110x.ko
    /usr/bin/wlan-cal > /dev/null 2>&1 # TODO: replace

    echo "done."
}

initialize_bluetooth() {
    echo -n "Initializing Bluetooth..."

	/usr/bin/bt-cal USER # TODO: replace

    echo "done."
}

mount_rootfs() {
    echo -n "Mounting rootfs..."

    _mountfs $ROOTFS_FSTYPE $ROOTFS_DEV $ROOTFS_DIR "$ROOTFS_OPTS"

    echo "done."
}

initialize_rootfs() {
    echo -n "Initializing root filesystem..."

    # is there a init?
    if [ ! -x "$ROOTFS_DIR/sbin/init" ]; then
        echo "/sbin/init not found on rootfs"
        umount $ROOTFS_DIR
        exit 1
    fi

    # mount a common /tmp
    mount -obind /tmp "$ROOTFS_DIR/tmp"

    # create a common /var/run in /tmp
    mkdir -p "$COMMON_ROOT/var/run"
    if [ -e "$ROOTFS_DIR/var/run" ]; then
        rm -rf "$ROOTFS_DIR/var/run"
    fi
    ln -sf "$COMMON_ROOT/var/run" "$ROOTFS_DIR/var/run"

    echo "done."
}

export_initfs_bin() {
    echo -n "Exporting binaries from initfs to rootfs..."
    cat /etc/linuxrc.export | while read f; do
        if [ -e ${ROOTFS_DIR}$f ]; then
            rm -rf "${ROOTFS_DIR}$f"
        fi
        ln -sf "${INITFS_DIR}$f" "${ROOTFS_DIR}$f"
    done
    echo "done."
}

boot() {
    echo "Booting..."

    # pivot
    cd $ROOTFS_DIR
    mkdir -p ".$INITFS_DIR"
    pivot_root . ".$INITFS_DIR"

    exec /usr/sbin/chroot . /sbin/init $RUNLEVEL  < dev/console > dev/console 2>&1
}

main

